package common.customer.dao;

import java.util.List;

import common.base.GenericHibernateDao;
import common.customer.bean.Customer;

@SuppressWarnings("unchecked")
public class CustomerDaoImpl extends GenericHibernateDao<Customer,Integer> implements CustomerDao {


	@Override
	public Customer getCustomerByUUID(String uuid) {
		List<Customer> list = this.getHibernateTemplate().find("from Customer c where c.uuid='"+uuid+"'");
		if(list!=null && list.size() >= 1){
			return list.get(0);
		}
		return null;
	}
	@Override
	public Customer getCustomerByNameAndPass(String name, String password) {
		List<Customer> list = this.getHibernateTemplate().find("from Customer c where c.userName='"+name+"' and c.passWord='"+password+"'" );
		if(list!=null && list.size() >= 1){
			return list.get(0);
		}
		return null;
	}
	@Override
	public Customer getCustomerByName(String name) {
		List<Customer> list = this.getHibernateTemplate().find("from Customer c where c.userName='"+name+"' ");
		if(list!=null && list.size() >= 1){
			return list.get(0);
		}
		return null;
	}
	@Override
	public int countCustomer() {
		return ((Long)this.getHibernateTemplate().find("select count(c.id) from Customer c").get(0)).intValue();
	}

	@Override
	public int countCustomerByCityId(int cityId) {
		return ((Long)this.getHibernateTemplate().find("select count(c.id) from Customer c where c.city.id="+cityId).get(0)).intValue();
	}

	@Override
	public int countCustomerByName(String name) {
		return ((Long)this.getHibernateTemplate().find("select count(c.id) from Customer c where c.userName='"+name+"'").get(0)).intValue();
	}
	@Override
	public boolean testNichengExist(int customerId, String nicheng) {
		long result = (Long)this.getHibernateTemplate().find("select count(c.id) from Customer c where c.id!="+customerId +" and c.nicheng='"+nicheng+"'").get(0);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}
}
